import { isObject } from '@vue/shared';

import defaultSetting from '@/config/setting';
import { SETTING_KEY } from '@/enums/cacheEnums';
import cache from '@/utils/cache';
import { setTheme } from '@/utils/theme';
import { defineStore } from 'pinia';

const storageSetting = cache.get(SETTING_KEY);

export const useSettingStore = defineStore('setting', {
  state: () => {
    const state = {
      showDrawer: false,
      mode: '',
      ...defaultSetting,
    };
    isObject(storageSetting) && Object.assign(state, storageSetting);
    return state;
  },
  actions: {
    // 设置布局设置
    setSetting(data: Record<string, any>) {
      const { key, value } = data;
      if (Object.prototype.hasOwnProperty.call(this, key)) {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        this[key] = value;
      }
      const settings: any = Object.assign({}, this.$state);
      delete settings.showDrawer;
      cache.set(SETTING_KEY, settings);
    },
    // 设置主题色
    setTheme(isDark: boolean) {
      this.mode = isDark ? 'dark' : 'light';
      setTheme(
        {
          primary: this.theme,
          success: this.successTheme,
          warning: this.warningTheme,
          danger: this.dangerTheme,
          error: this.errorTheme,
          info: this.infoTheme,
        },
        isDark,
        this.subTheme,
      );
    },
    resetTheme() {
      for (const key in defaultSetting) {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        this[key] = defaultSetting[key];
      }
      cache.remove(SETTING_KEY);
    },
  },
});

export default useSettingStore;
